var t = require("../../../@babel/runtime/helpers/interopRequireDefault"), a = require("../../../@babel/runtime/helpers/toConsumableArray"), e = t(require("../../../@babel/runtime/regenerator")), s = require("../../../@babel/runtime/helpers/asyncToGenerator"), o = require("../common.js"), n = getApp();

Page({
    behaviors: [ o ],
    data: {
        active: "-1",
        keyWord: "",
        isBatchOperation: !1,
        allCheck: !1,
        goodsList: [],
        goodsCount: 0,
        batchGoods: [],
        batchGoodsAll: [],
        optionBtns: [ {
            text: "批量上架",
            type: "up"
        }, {
            text: "批量下架",
            type: "down"
        }, {
            text: "批量删除",
            type: "delete"
        } ],
        page: 1,
        isLoadMore: !0,
        total_page: 0,
        networkLoading: !1
    },
    onLoad: function(t) {
        this.getGoodsList();
    },
    initData: function() {
        this.setData({
            page: 1,
            total_page: 0,
            isLoadMore: !0,
            networkLoading: !1
        });
    },
    handSearch: function(t) {
        this.data.keyWord = t.detail, this.getGoodsList();
    },
    getGoodsList: function() {
        var t = this;
        this.initData();
        var a = {
            page: this.data.page,
            search: {
                title: this.data.keyWord
            }
        };
        "-1" !== this.data.active && (a.search.status = this.data.active);
        var e = n.getNetAddresss("plugin.shop-assistant.frontend.goods.get-list");
        n._postNetWork({
            url: e,
            data: a,
            success: function(a) {
                var e = a.data;
                if (1 != e.result) return n.tips(e.msg);
                if (t.data.total_page = e.data.last_page, t.data.total_page || (t.data.total_page = 0), 
                e.data.data) {
                    var s = e.data.data.map(function(t) {
                        return t.id + "";
                    });
                    t.data.batchGoodsAll = s;
                }
                t.setData({
                    goodsList: e.data.data,
                    goodsCount: e.data.total,
                    networkLoading: !0
                });
            }
        });
    },
    selectBolfun: function() {
        this.setData({
            isBatchOperation: !0
        });
    },
    cancelBatchOperation: function() {
        this.setData({
            isBatchOperation: !1
        });
    },
    checkboxGroupChange: function(t) {
        this.setData({
            batchGoods: t.detail
        });
    },
    toggleAll: function(t) {
        var a = t.detail, e = [];
        a && (e = JSON.parse(JSON.stringify(this.data.batchGoodsAll))), this.setData({
            allCheck: a,
            batchGoods: e
        });
    },
    changeTabs: function(t) {
        this.data.active = t.detail.name, this.getGoodsList();
    },
    onShelf: function(t) {
        var a = this;
        return s(e.default.mark(function s() {
            var o, r, i, d;
            return e.default.wrap(function(e) {
                for (;;) switch (e.prev = e.next) {
                  case 0:
                    return o = t.currentTarget.dataset.goodsid, r = t.currentTarget.dataset.option, 
                    i = r ? "上架" : "下架", e.next = 5, n.confirm("是否".concat(i, "该商品"));

                  case 5:
                    if (e.sent) {
                        e.next = 8;
                        break;
                    }
                    return e.abrupt("return");

                  case 8:
                    d = n.getNetAddresss("plugin.shop-assistant.frontend.goods.update-on-shelf"), n._postNetWork({
                        url: d,
                        data: {
                            id: o,
                            status: r
                        },
                        success: function(t) {
                            var e = t.data;
                            n.tips(e.msg), 1 == e.result && a.getGoodsList();
                        }
                    });

                  case 10:
                  case "end":
                    return e.stop();
                }
            }, s);
        }))();
    },
    deleteGoods: function(t) {
        var a = this;
        return s(e.default.mark(function s() {
            var o, r;
            return e.default.wrap(function(e) {
                for (;;) switch (e.prev = e.next) {
                  case 0:
                    return o = t.currentTarget.dataset.goodsid, e.next = 3, n.confirm("是否删除该商品");

                  case 3:
                    if (e.sent) {
                        e.next = 6;
                        break;
                    }
                    return e.abrupt("return");

                  case 6:
                    r = n.getNetAddresss("plugin.shop-assistant.frontend.goods.delete"), n._postNetWork({
                        url: r,
                        data: {
                            id: o
                        },
                        success: function(t) {
                            var e = t.data;
                            n.tips(e.msg), 1 == e.result && a.getGoodsList();
                        }
                    });

                  case 8:
                  case "end":
                    return e.stop();
                }
            }, s);
        }))();
    },
    handlerBatchOpt: function(t) {
        var a = t.currentTarget.dataset.item;
        if (0 === this.data.batchGoods.length) return n.tips("请先选择需要批量操作的商品");
        "up" === a.type ? this.batchOnShelf(1) : "down" === a.type ? this.batchOnShelf(0) : this.batchDelte();
    },
    batchOnShelf: function(t) {
        var a = this;
        return s(e.default.mark(function s() {
            var o, r;
            return e.default.wrap(function(e) {
                for (;;) switch (e.prev = e.next) {
                  case 0:
                    return o = t ? "上架" : "下架", e.next = 3, n.confirm("正在进行批量".concat(o, "操作，是否继续？"));

                  case 3:
                    if (e.sent) {
                        e.next = 6;
                        break;
                    }
                    return e.abrupt("return");

                  case 6:
                    r = n.getNetAddresss("plugin.shop-assistant.frontend.goods.batch-update-shelf"), 
                    n._postNetWork({
                        url: r,
                        data: {
                            ids: a.data.batchGoods,
                            status: t
                        },
                        success: function(t) {
                            var e = t.data;
                            n.tips(e.msg), 1 == e.result && (a.getGoodsList(), a.setData({
                                batchGoods: []
                            }));
                        }
                    });

                  case 8:
                  case "end":
                    return e.stop();
                }
            }, s);
        }))();
    },
    batchDelte: function() {
        var t = this;
        return s(e.default.mark(function a() {
            var s;
            return e.default.wrap(function(a) {
                for (;;) switch (a.prev = a.next) {
                  case 0:
                    return a.next = 2, n.confirm("正在进行批量删除操作，是否继续？");

                  case 2:
                    if (a.sent) {
                        a.next = 5;
                        break;
                    }
                    return a.abrupt("return");

                  case 5:
                    s = n.getNetAddresss("plugin.shop-assistant.frontend.goods.batch-delete"), n._postNetWork({
                        url: s,
                        data: {
                            ids: t.data.batchGoods
                        },
                        success: function(a) {
                            var e = a.data;
                            n.tips(e.msg), 1 == e.result && (t.getGoodsList(), t.setData({
                                batchGoods: []
                            }));
                        }
                    });

                  case 7:
                  case "end":
                    return a.stop();
                }
            }, a);
        }))();
    },
    onReady: function() {},
    onShow: function() {},
    onHide: function() {},
    onUnload: function() {},
    onPullDownRefresh: function() {},
    onReachBottom: function() {
        this.data.isLoadMore && this._getMoreData();
    },
    _getMoreData: function() {
        var t = this;
        if (this.data.isLoadMore = !1, !(this.data.page >= this.data.total_page)) {
            this.data.page += 1;
            var e = {
                page: this.data.page,
                search: {
                    title: this.data.keyWord
                }
            };
            "-1" !== this.data.active && (e.search.status = this.data.active);
            var s = n.getNetAddresss("plugin.shop-assistant.frontend.goods.get-list");
            n._postNetWork({
                url: s,
                data: e,
                success: function(e) {
                    var s = e.data;
                    if (t.data.isLoadMore = !0, 1 === s.result) {
                        if (s.data.data) {
                            var o, n = s.data.data.map(function(t) {
                                return t.id + "";
                            });
                            (o = t.data.batchGoodsAll).push.apply(o, a(n));
                        }
                        var r = t.data.goodsList.concat(s.data.data);
                        t.setData({
                            goodsList: r
                        });
                    } else t.data.page = t.data.page - 1, t.data.isLoadMore = !1;
                }
            });
        }
    },
    onShareAppMessage: function() {}
});